package club.wx3609.images.service;

import club.wx3609.images.Utils.IsNullUtils;
import club.wx3609.images.entity.User;
import club.wx3609.images.interfaces.repository.UserRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    public User save(User user){
        return userRepository.save(user);
    }
    public void delete(User user){
        userRepository.delete(user);
    }

    public User loginService(String userNumber,String userPassword){
        User user=null;
        if(IsNullUtils.character(userNumber)&&IsNullUtils.character(userPassword)){
            user=userRepository.findByUserNumberAndUserPassword(userNumber,userPassword);
        }
        return user;
    }

    public User loginService(Integer userId){
        return userRepository.getOne(userId);
    }

    public User registerVerification(String userNumber){
        return userRepository.findByUserNumber(userNumber);
    }

    public User registerUser(User user){
        return userRepository.save(user);
    }

    public List<User> findAll(){
        return userRepository.findAll();
    }

    /**
     * 分页查询
     * @param pageNumber 当前页码
     * @param pageSize 每页所占的容量
     * @param sortTitle 给哪列排序
     */
    public Page<User> findAllUser(Integer pageNumber, Integer pageSize, String sortTitle){
        Sort sort = new Sort(Sort.Direction.DESC, sortTitle);
        Pageable pageable = new PageRequest(pageNumber, pageSize, sort);
        Page<User> page=userRepository.findAll(pageable);
        return page;
    }
}
